<?php
require_once '../application/modules/admin/models/Clients.php';
require_once '../application/modules/admin/models/Groups.php';
require_once '../application/modules/admin/models/Businesses.php';
require_once '../application/modules/survey/models/SurveysCompleted.php';
require_once '../application/modules/sponsor/models/DonationAllocations.php';
require_once 'Applications.php';
require_once 'LoanProducts.php';
require_once 'Loans.php';
require_once 'LoanMasters.php';
require_once 'LoanStatusChanges.php';

class Loan_ApplicationController extends HTech_Controller_Action
{
    function init()
    {
        $cfg = array(
            'model' => 'Applications', 
            'form' => 'Emms_Form_Loan_Application',
            'config' => array('new' => 'loan_masters'), // already set as default table in the model
            'redirect' => array('view','application','loan'),
            'flashMessenger' => true
        );
        parent::init($cfg);   
    }
    
    public function newAction()
    {
        parent::addAction();
        $this->view->Borrower = $this->view->form->getBorrower();
    }
    
    public function editAction()
    {
        $loans = new Loans();
        $this->view->loan = $loans->find((int) $this->_request->getParam('loan'))->current(); 
        
        if(!$this->view->loan) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        
        switch($this->view->loan->Status) {
            case 'New': 
                break;
            case 'Rejected': 
                break;
            default:
                $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
                $this->redirect('/default/auth/deny'); 
        }
        
        $this->view->form = new Emms_Form_Loan_ApplicationLoan($this->view->loan);
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues();
                $loans = new Loans(array('name' => 'loans'));
                $loans->save($data);
                $app = new Emms_LoanApplication($this->view->loan->LoanMasterId);
                $app->sync();
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->loan->LoanMasterId)); 
            }
        }
        $this->_helper->layout->setLayout('form');
    }
    
    public function deleteAction()
    {
        if($this->_request->getParam('loan')) {
            try {
                $app = new Emms_LoanApplication(null,$this->_request->getParam('loan'));
                $masterId = $app->delete($this->_request->getParam('loan'));
            } catch (Exception $e) {
                $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                $this->redirect('/default/auth/deny'); 
            }
            if($masterId > 0) 
                $this->redirect('/loan/application/view/id/' . $masterId);
                
            $this->getHelper('flashMessenger')
                ->addMessage($this->view->translate("Loan application deleted!"));
            $this->redirect('/default/auth/info');
        }
    }
    
    public function viewAction()
    {
        try {
            $app = new Emms_LoanApplication((int) $this->_request->getParam('id'));
        } catch (Exception $e) {
            $this->getHelper('flashMessenger')->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        
        $this->view->Menu = new Emms_LoanStatus($app->getMaster()->Status);
        if($this->view->Menu->isViewOkay()) {
            parent::viewAction();
            $this->view->SurveysCompleted = $app->surveysCompleted();
            $this->view->SubmitReady = $app->isSubmitReady();
        } else {
            $this->redirect('/loan/loan-master/view/id/' . $app->getLoanMasterId());
        }
    }
    
    public function changeDatesAction()
    {
        $this->_doAction('changeDates','Emms_Form_Loan_ChangeDates',$this->view->translate("Change tentative dates"),true);
    }
    
    public function submitAction()
    {
        $this->_doAction('submit','Emms_Form_Loan_Comment',$this->view->translate("Submit"));
    }
    
    public function reviewAction()
    {
        $this->_doAction('review','Emms_Form_Loan_Comment',$this->view->translate("Review"));
    }
        
    public function approveAction()
    {
        $this->_doAction('approve','Emms_Form_Loan_Comment',$this->view->translate("Approve"));
    }
        
    public function executeAction()
    {
        $this->_doAction('execute','Emms_Form_Loan_Execute',$this->view->translate("Execute check"),true);
    }
        
    public function disburseAction()
    {
        $this->_doAction('disburse','Emms_Form_Loan_Disburse',$this->view->translate("Disburse loan"),true);
    }
        
    public function withdrawAction()
    {
        $this->_doAction('withdraw','Emms_Form_Loan_Comment',$this->view->translate("Withdraw"));
    }
        
    public function rejectAction()
    {
        $this->_doAction('reject','Emms_Form_Loan_Comment',$this->view->translate("Reject"));
    }
    
    private function _doAction($action,$form,$formDescription,$masterParam=false)
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        
        $this->view->form = $masterParam ? new $form($this->view->master) : new $form();    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->$action($data);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($formDescription);  
        $this->_helper->layout->setLayout('form');   
    }
    
    /*
    
    
    public function changeDatesAction()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_ChangeDates($this->view->master);      
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->changeDates($data);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->_helper->layout->setLayout('form');
    }
    
     public function submitAction()
    {
        try {
            $app = new Emms_LoanApplication((int) $this->_request->getParam('id'));
            $app->submit();
        } catch (Exception $e) {
            $this->getHelper('flashMessenger')->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->redirect('/loan/loan-master/view/id/' . $app->getLoanMasterId());
    }
    
    public function review2Action()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_Comment();    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->review($data['Comments']);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($this->view->translate("Review"));  
        $this->_helper->layout->setLayout('form');        
    }
    
    public function approve2Action()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_Comment();    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->approve($data['Comments']);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($this->view->translate("Approve"));  
        $this->_helper->layout->setLayout('form');    
    }
    
    public function execute2Action()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_Execute($this->view->master);    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->execute($data);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($this->view->translate("Execute check"));  
        $this->_helper->layout->setLayout('form');   
    }
    
    public function disburse2Action()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_Disburse($this->view->master);    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues();
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->disburse($data);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($this->view->translate("Disburse loan"));  
        $this->_helper->layout->setLayout('form'); 
    }
    
    public function withdraw2Action()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_Comment();    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->withdraw($data['Comments']);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($this->view->translate("Withdraw"));  
        $this->_helper->layout->setLayout('form');  
    }
    
    public function reject2Action()
    {
        $masters = new LoanMasters();
        $this->view->master = $masters->find((int) $this->_request->getParam('id'))->current(); 
        
        if (!$this->view->master) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new Emms_Form_Loan_Comment();    
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues(); 
                try {
                    $app = new Emms_LoanApplication($this->view->master->LoanMasterId);
                    $app->reject($data['Comments']);
                } catch (Exception $e) {
                    $this->getHelper('flashMessenger')->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->master->LoanMasterId));
            }
        }
        $this->view->form->setDescription($this->view->translate("Reject"));  
        $this->_helper->layout->setLayout('form');  
    }
     * 
     */
}
